package com.chlitina.store.modules.cash.model;

import java.util.Date;

import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.cash.entity.StoreCardDuration;
import com.chlitina.store.modules.cash.model.recharge.CardDurationRecharge;

public class DurationCardPayment extends CardPayment {
	public DurationCardPayment(StoreCardDuration card, long id) {
		super(card, id);
		setNewCard(getCard().getId() == null);
	}

	public void reset() {
		super.reset();
		rechargedDuration = 0;
	}

	/**
	 * 本次共充入的 duration
	 */
	private int rechargedDuration;

	public StoreCardDuration getCard() {
		return (StoreCardDuration) card;
	}

	@Override
	public void payMoney() {
		super.payMoney(getRealCost());
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(super.toString());
		sb.append(", rechargedDuration= " + getCurrentMoney());
		return sb.toString();
	}

	public void recharge(CardDurationRecharge recharge) {
		System.err.println("-----------");
		System.err.println("Recharge:" + recharge.toString());
		System.err.println("Before Recharge:" + toString());
		//
		int duration = recharge.getDuration();
		rechargedDuration += duration;
		// FIXME money 是否需要被记录并更新
		System.err.println("End Recharge:" + toString());
	}

	public Date getLimitTime() {
		return DateUtils.appendMonth(getCard().getEndTime(), rechargedDuration);
	}

	public boolean isValid() {
		if (getLimitTime().getTime() < System.currentTimeMillis()) {
			return false;
		}
		return true;
	}

	public int getRechargedDuration() {
		return rechargedDuration;
	}

	public void setRechargedDuration(int rechargedDuration) {
		this.rechargedDuration = rechargedDuration;
	}

}
